<!DOCTYPE html>
<!--

share.html

code for sharing thought patches

Copyright (C) 2009, Chris Allert, All rights reserved.

This program is free software; you can redistribute it and/or modify it under 
the terms of the GNU General Public License as published by the Free Software 
Foundation; either version 3 of the License, or (at your option) any later 
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY 
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with 
this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
Place, Suite 330, Boston, MA 02111-1307 USA

The latest version of this file can be downloaded from
http://thought-patches.sourceforge.net/

-->
<html>
	<head>
		<title>save/share</title>
<link rel="apple-touch-icon" href="../../thought-patches.png">
<meta content="width=device-width; initial-scale=1.0;  maximum-scale=1.0; user-scalable=0;" name="viewport" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection" content="telephone=no" />
<style>
	body { padding:0px; margin:0px; color: white; background-color: black; font-size: 16px }
	input { 
		font-size:16px;
		padding: 0px;
	}
	select { 
		font-size:16px;
		padding: 0px;
	}
	* {font-family: sans-serif}
	form { padding:0px; margin:0px; }
</style>
		<script src="../../lib/query.js"></script>
		<script src="../../lib/short.js"></script>
		<script>
			var share_url;
			var mail_url;
			var back_url;
			var my_title;
			var p;

			var labels = {
				"cartwheel-tiling": "cartwheel",
				"hypotrochoid": "hypotrochoid",
				"epitrochoid": "epitrochoid",
				"repeating-decimal": "repeating decimal"
			};

			function init() {
				var message = document.getElementById('message');
				my_title = document.getElementById('my_title');
				var patch = document.getElementById('patch');
				query_init();
				p = query['patch'][0];
				var u = ("" + document.location).split('?')[0];
				if (0 == u.search('file:')) {
					// reset to a real url if they are trying to share something local
					share_url = "http://thoughtpatches.googlecode.com/svn/trunk/patches/" + p + "/" + p + ".html" 
					mail_url = "http://thoughtpatches.org/p/";
				} else {
					share_url = u.replace('share/share.html', 
					p + '/' + p + '.html');
					mail_url = u.replace('share/share.html', 
					'patch/patch.html?s=');
				}
				mail_url += pack_patch(query);
				var a = [];
				for (var i in query) {
					if ('title' == i) { continue; }
					if ('patch' == i) { continue; }
					a.push(encodeURIComponent(i) +
					'=' + encodeURIComponent(query[i][0]));
				}
				share_url += '?' + a.join('&');
				back_url = '../' + p + '/' + p + '.html?' + a.join('&');
				document.getElementById('back_link').innerHTML = 'see it again';// + labels[p];
				if (null == labels[p]) {labels[p] = 'patch'; }
				patch.innerHTML = labels[p];
				if (null != query['title']) {
					my_title.value = escape_html(query['title'][0]);
				} else {
					my_title.value = "my " + labels[p];
				}
				update_share_urls();
			}

			function escape_html(s) {
				if (null == s) { return "null" }
				var o = s.replace('&', '&amp;');
				o = o.replace('<', '&lt;');
				o = o.replace('>', '&gt;');
				o = o.replace('"', '&quot;');
				o = o.replace("'", '&apos;');
				return o;
			}

			function go_back() { go_to(back_url); }
			function go_to(u) {
				// have to do this so iphone web app doesn't
				// open new safari window
				document.location = u;
			}

			function update_share_urls() {
				var facebook_link = document.getElementById('facebook_link');
				var twitter_link = document.getElementById('twitter_link');
				var mail_link = document.getElementById('mail_link');

				var u = share_url;
				var t = my_title.value;
				var s = (
				'http://www.facebook.com/sharer.php?t=' +
				encodeURIComponent(t) + '&u=' +
				encodeURIComponent(u));
				facebook_link.innerHTML = '<a href="' + s + '" target="_blank" style="color: orange; ">facebook</a>';

				u = "http://thoughtpatches.org/p/" + pack_patch(query);
				twitter_link.innerHTML = '<a href="' + u + '">packed</a>';
				s = (
				'http://twitter.com/home?status=' +
				encodeURIComponent(u));
				twitter_link.innerHTML = '<a href="' + s + '" target="_blank" style="color: yellow; ">twitter</a>';

				s = (
					'mailto:?subject=' + 
					encodeURIComponent(t) +
					'&body=' + encodeURIComponent(mail_url)
				);
				mail_link.innerHTML = '<a href="' + s + '" target="_blank" style="color: red; ">e-mail</a>';
			}

			function patch_save() {
				var a = [];
				for (var i in query) {
					if ('title' == i) { continue; }
					a.push( encodeURIComponent(i) + 
					"=" + encodeURIComponent(query[i][0])); 
				}
				a.push('title=' + 
					encodeURIComponent(my_title.value));
				document.location = 
					'../save/save.html?' + a.join('&');
			}

		</script>
	</head>
	<body onLoad="init()">
		<table width="320">
			<tr><td align="center"><strong>save/share your <span id="patch">patch</span></td></tr>
			<tr><td>&nbsp;</td></tr>
			<tr><td align="center">
				<div style="font-size: 24px;">name it:</div>
				<input name="my_title" id="my_title" onChange="update_share_urls()" />
			</td></tr>
			<tr><td>&nbsp;</td></tr>
			<tr><td align="center"><span style="color: aquamarine; text-decoration: underline; font-size: 24px; " onClick="patch_save()">save it</span></td></tr>
			<tr><td>&nbsp;</td></tr>
			<tr><td align="center" style="color: lime; font-size: 24px;" >share it:</td></tr>
			<tr><td align="center">
				<span id="mail_link" style="font-size: 24px"></span>
				|
				<span id="facebook_link" style="font-size: 24px"></span>
				|
				<span id="twitter_link" style="font-size: 24px"></span>
			</td></tr>
			<tr><td>&nbsp;</td></tr>
			<tr><td align="center"><span id="back_link" style="color: white; text-decoration: underline; font-size: 24px; " onClick="go_back()">save it</span></td></tr>
		</table>
	</body>
</html>
